package avicit.bdp.dcs.threshold.other;

import avicit.bdp.dcs.stream.dto.MeasureParaDto;
import avicit.bdp.dcs.stream.dto.MsgTemplateDto;
import avicit.bdp.dcs.threshold.utils.TestUtils;
import avicit.bdp.dcs.tools.enums.MeasureParaTypeEnum;
import avicit.platform6.commons.utils.ComUtil;
import avicit.platform6.core.rest.msg.ResponseMsg;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class MsgTemplateTest {
    private RestTemplate restTemplate = new RestTemplate();

    private static String paramName = "paramName_" + System.currentTimeMillis();

    /**
     * 所有用例执行前准备工作
     */
    @BeforeClass
    public static void init() {
        System.out.println("用例初始化...");
    }

    /**
     * 单个测试用例运行前准备工作
     */
    @Before
    public void setUp() {
        // TODO:
    }

    /**
     * 单个测试用例运行后销毁工作
     */
    @After
    public void tearDown() {
        // TODO:
    }

    /**
     * 所有用例执行完成后准备工作
     */
    @AfterClass
    public static void destory() {
        System.out.println("用例执行完成，开始清理...");
    }

    /***********************************************Begin 测试用例 ***********************************************/
    @Test(timeout = 5 * 60 * 1000)
    public void testFile() throws Exception {
        // 新增消息模板
        String deviceId = ComUtil.getId();
        JSONObject param = new JSONObject();
        param.put("deviceId", deviceId);
        param.put("deviceName", ComUtil.getId());

        ResponseEntity<ResponseMsg> result = restTemplate.postForEntity(TestUtils.HOSTPORT + "/api/bdp/dcs/stream/template/createMsgTemplate",
                TestUtils.buildPostHttpEntity(param.toJSONString()), ResponseMsg.class);

        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());

        // 获取模板列表
        result = restTemplate.exchange(TestUtils.HOSTPORT + "/api/bdp/dcs/stream/template/getMsgTemplateList?sort=1",
                HttpMethod.GET, TestUtils.buildGetHttpEntity(), ResponseMsg.class);

        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());

        LinkedHashMap<String, Object> map = (LinkedHashMap<String, Object>) result.getBody().getResponseBody();
        List<Object> finalBody = (List<Object>) map.get("result");
        String templateId = "";
        for (Object obj : finalBody) {
            LinkedHashMap<String, String> templateObj = (LinkedHashMap<String, String>) obj;
            MsgTemplateDto template = JSON.parseObject(JSON.toJSONString(templateObj), MsgTemplateDto.class);
            if (template.getDeviceId().equals(deviceId)) {
                System.out.println("设备ID:" + template.getDeviceId());
                templateId = template.getId();
                break;
            }
        }

        // 修改模板
        param.put("id", templateId);
        param.put("deviceId", deviceId);
        param.put("deviceName", ComUtil.getId());
        result = restTemplate.postForEntity(TestUtils.HOSTPORT + "/api/bdp/dcs/stream/template/updateMsgTemplate",
                TestUtils.buildPostHttpEntity(param.toJSONString()), ResponseMsg.class);

        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());

        // 新增模板参数
        MsgTemplateDto msgTemplateDto = new MsgTemplateDto();
        List<MeasureParaDto> measureParaDtoList = new ArrayList<>();
        MeasureParaDto measureParaDto = new MeasureParaDto();
        measureParaDto.setName(paramName);
        measureParaDto.setRemark("aa");
        measureParaDto.setType(MeasureParaTypeEnum.STRING);
        measureParaDtoList.add(measureParaDto);
        msgTemplateDto.setMeasureParaDtoList(measureParaDtoList);
        msgTemplateDto.setId(templateId);

        String param1 = JSONObject.toJSONString(msgTemplateDto);
        result = restTemplate.postForEntity(TestUtils.HOSTPORT + "/api/bdp/dcs/stream/template/addMsgTemplateParas",
                TestUtils.buildPostHttpEntity(param1), ResponseMsg.class);

        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());

        // 查询模板参数
        result = restTemplate.exchange(TestUtils.HOSTPORT + "/api/bdp/dcs/stream/template/getMsgTemplateParas?id="+templateId+"&sort=1",
                HttpMethod.GET, TestUtils.buildGetHttpEntity(), ResponseMsg.class);

        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());

        finalBody = (List<Object>) result.getBody().getResponseBody();
        String paramId = "";
        for (Object obj : finalBody) {
            LinkedHashMap<String, String> paramObj = (LinkedHashMap<String, String>) obj;
            MeasureParaDto paramDto = JSON.parseObject(JSON.toJSONString(paramObj), MeasureParaDto.class);
            if (paramDto.getName().equals(paramName)) {
                System.out.println("参数名称:" + paramDto.getName());
                paramId = paramDto.getId();
                break;
            }
        }

        // 修改模板参数
        MsgTemplateDto msgTemplateDto1 = new MsgTemplateDto();
        List<MeasureParaDto> measureParaDtoList1 = new ArrayList<>();
        MeasureParaDto measureParaDto1 = new MeasureParaDto();
        measureParaDto1.setId(paramId);
        measureParaDto1.setName(paramName);
        measureParaDto1.setRemark("aa");
        measureParaDto1.setType(MeasureParaTypeEnum.STRING);
        measureParaDtoList1.add(measureParaDto1);
        msgTemplateDto1.setMeasureParaDtoList(measureParaDtoList1);
        msgTemplateDto1.setId(templateId);

        param1 = JSONObject.toJSONString(msgTemplateDto1);
        result = restTemplate.postForEntity(TestUtils.HOSTPORT + "/api/bdp/dcs/stream/template/updateMsgTemplateParas",
                TestUtils.buildPostHttpEntity(param1), ResponseMsg.class);

        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());

        // 删除模板
        JSONObject param2 = new JSONObject();
        param2.put("id", templateId);
        result = restTemplate.exchange(TestUtils.HOSTPORT + "/api/bdp/dcs/stream/template/deleteMsgTemplate",
                HttpMethod.DELETE, TestUtils.buildDeleteHttpEntity(param2.toJSONString()), ResponseMsg.class);

        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());
    }

}
